<?php

/*

Process Fight (battle_1p.pro.php)

*/
ob_start();
$rank_check = 1;
include "global.inc.php";



$findBattle = fetch("SELECT * FROM one_player_battles2 WHERE userid = '$userid' AND game = '$game'");
if (!$findBattle[id])
{
	die(header(error("battle_1player.php?game=$game","This fight does not exsist.")));
}

$findEnemy = fetch("SELECT * FROM one_players2 WHERE id = '$findBattle[enemy]' AND game = '$game'");
$findYourPet = fetch("SELECT * FROM user_pets2 WHERE id = '$findBattle[your_pet]' AND game = '$game'");
$findYourSpecies = fetch("SELECT * FROM pets2 WHERE id = '$findYourPet[species]' AND game = '$game'");

if ($findBattle[enemy_hp] <= 0)
{
	die("The fight is over already, please go back.");
}

if ($findYourPet[current_hp] <= 0)
{
	die("The fight is over already, please go back.");
}

if (($findYourPet[current_hp] <= 0) AND ($findBattle[enemy_hp] <= 0))
{
	die("The fight is over already, please go back.");
}

if ($action == "attack")
{
	$findAttack = fetch("SELECT * FROM attacks2 WHERE attack_name = '$use_attack' AND game = '$game'");

	if ($use_attack == "charge") { $findAttack[do_what] = "attack"; $findAttack[magic_number] = "1"; }
	if ($use_attack == "defend") { $findAttack[do_what] = "defend"; $findAttack[magic_number] = "1"; }
	if ($findAttack[do_what] == "attack")
	{
		$hit_or_not = rand(1,100);
		if ($hit_or_not > $findBattle[y_acc])
		{
			$status_line .= "<font color=\"$topAndBottomBG\">You tried to use $findAttack[attack_name] on $findEnemy[name], but you missed.</font><br>";
		}
		if ($hit_or_not <= $findBattle[y_acc])
		{
			$randD1 = rand(1,2);
			$randD2 = rand(0,9);
			$divider = "$randD1.$randD2";
			$randD2 = rand(1,2);
			$randD2 = rand(0,9);
			$divider2 = "$randD1.$randD2";

			$do_damage = round(((($findYourPet[strength] * .65) * $findAttack[magic_number]) 	* $divider) * .6);
			$do_damage = $do_damage - floor(($findEnemy[defense] * .4) * $divider2);
			$do_damage = round($do_damage / 2.5);

			if ($do_damage < 1) { $do_damage = 1; }
			$status_line .= "<font color=\"$topAndBottomBG\">You used $findAttack[attack_name] and attacked $findEnemy[name] and did $do_damage points of damage.</font><br>";
		}
	}
	if ($findAttack[do_what] == "defend")
	{
		$hit_or_not2 = rand(1,100);
		if ($hit_or_not2 > $findBattle[y_acc])
		{
			$status_line .= "<font color=\"$topAndBottomBG\">You tried to use $findAttack[attack_name], but you failed.</font><br>";
		}
		if ($hit_or_not2 <= $findBattle[y_acc])
		{
			$randD1 = rand(1,2);
			$randD2 = rand(1,9);
			$divider = "$randD1.$randD2";
			$takeOffExtra = round($findAttack[magic_number] * $divider);
			$status_line .= "<font color=\"$topAndBottomBG\">You used $findAttack[attack_name] and defended yourself from $takeOffExtra points of damage.</font><br>";
		}
	}
}

if ($action == "item")
{
	$find_item = fetch("SELECT * FROM equipped_items2 WHERE id = '$use_item' AND pet_id = '$findYourPet[id]' AND game = '$game'");
	$find_item2 = fetch("SELECT * FROM items2 WHERE id = '$find_item[item_id]' AND game = '$game'");

	if (!$find_item[id])
	{
		die(header(error("battling_1p.php?game=$game","That is not your item.")));
	}

	if ($find_item2[item_type] == "health_potion")
	{
		$new_health = $findYourPet[current_hp] + $find_item2[magic_num];
		if ($new_health > $findYourPet[max_hp]) { $new_health = $findYourPet[max_hp]; }
		$healPoints = $new_health - $findYourPet[current_hp];

		mysql_query("UPDATE user_pets2 SET current_hp = '$new_health' WHERE owner = '$userid' AND id = '$findYourPet[id]' AND game = '$game'");
		$findYourPet = fetch("SELECT * FROM user_pets2 WHERE id = '$findBattle[your_pet]' AND game = '$game'");

		if ($find_item[parts] <= 1)
		{
			mysql_query("DELETE FROM equipped_items2 WHERE id = '$find_item[id]' AND pet_id = '$findYourPet[id]' AND game = '$game'");
		}
		else
		{
			mysql_query("UPDATE equipped_items2 SET parts=$find_item[parts]-1 WHERE pet_id = '$findYourPet[id]' AND id = '$find_item[id]' AND game = '$game'");
		}
		$status_line .= "<font color=\"$topAndBottomBG\">You used your $find_item2[item_name] and healed $healPoints points.</font><br>";
	}
	if ($find_item2[item_type] == "battle_item")
	{
		$hit_or_not = rand(1,100);
		if ($hit_or_not > $findBattle[y_acc])
		{
			$status_line .= "<font color=\"$topAndBottomBG\">You tried to use your $find_item2[item_name] on $findEnemy[name], but you failed.</font><br>";
		}
		else
		{
			$findBattleItem = fetch("SELECT * FROM battle_items2 WHERE item_id = '$find_item[item_id]' AND game = '$game'");
			if ($findBattleItem[battle_type] == "attack")
			{
				$randD1 = rand(1,2);
				$randD2 = rand(0,9);
				$divider = "$randD1.$randD2";
				$randD2 = rand(1,2);
				$randD2 = rand(0,9);
				$divider2 = "$randD1.$randD2";

				$do_damage = round(((($findYourPet[strength] * .65) * $findBattleItem[magic_num]) * $divider) * .6);
				$do_damage = $do_damage - floor(($findEnemy[defense] * .4) * $divider2);
				$do_damage = round($do_damage / 2.5);

				if ($do_damage < 1) { $do_damage = 1; }
				$status_line .= "<font color=\"$topAndBottomBG\">You used your $find_item2[item_name] and attacked $findEnemy[name] and did $do_damage points of damage.</font><br>";
			}
			if ($findBattleItem[battle_type] == "defence")
			{
				$randD1 = rand(1,2);
				$randD2 = rand(1,9);
				$divider = "$randD1.$randD2";
				$takeOffExtra = round($findBattleItem[magic_num] * $divider);
				$status_line .= "<font color=\"$topAndBottomBG\">You used your $find_item2[item_name] and defended yourself from $takeOffExtra points of damage.</font><br>";
			}
			if ($findBattleItem[battle_type] == "lower_acc")
			{
				mysql_query("UPDATE one_player_battles2 SET e_acc=$findBattle[e_acc]-$findBattleItem[magic_num] WHERE userid = '$userid' AND game = '$game'");
				$status_line .= "<font color=\"$topAndBottomBG\">You used your $find_item2[item_name] and lowered your enemies accuracy by $findBattleItem[magic_num]%</font><br>";
			}
			if ($findBattleItem[battle_type] == "raise_acc")
			{
				mysql_query("UPDATE one_player_battles2 SET y_acc=$findBattle[y_acc]+$findBattleItem[magic_num] WHERE userid = '$userid' AND game = '$game'");
				$status_line .= "<font color=\"$topAndBottomBG\">You used your $find_item2[item_name] and raised your accuracy by $findBattleItem[magic_num]%</font><br>";
			}
			if ($findBattleItem[battle_type] == "double_damage")
			{
				$randD1 = rand(2,4);
				$randD2 = rand(0,9);
				$divider = "$randD1.$randD2";
				$randD2 = rand(1,2);
				$randD2 = rand(0,9);
				$divider2 = "$randD1.$randD2";

				$do_damage = round(((($findYourPet[strength] * 1.3) * $findBattleItem[magic_num]) * $divider) * 1.2);
				$do_damage = $do_damage - floor(($findEnemy[defense] * .4) * $divider2);
				$do_damage = round($do_damage / 2.5);
				$status_line .= "<font color=\"$topAndBottomBG\">You used your $find_item2[item_name] and attacked $findEnemy[name] with $do_damage points of damage.</font><br>";
			}
		}
		if (($find_item[parts] != "b") AND ($find_item[parts] != "i") AND ($find_item[parts] <= "1"))
		{
			mysql_query("DELETE FROM equipped_items2 WHERE pet_id = '$findYourPet[id]' AND id = '$find_item[id]' AND game = '$game'");
			$status_line .= "<font color=\"$topAndBottomBG\">Your $find_item2[item_name] can no longer be used.</font><br>";
		}
		if (($find_item[parts] != "b") AND ($find_item[parts] != "i") AND ($find_item[parts] > "1"))
		{
			mysql_query("UPDATE equipped_items2 SET parts=$find_item[parts]-1 WHERE pet_id = '$findYourPet[id]' AND id = '$find_item[id]' AND game = '$game'");
		}
		if ($find_item[parts] == "b")
		{
			$breakOrNot = rand(1,25);
			if ($breakOrNot >= 14)
			{
				mysql_query("DELETE FROM equipped_items2 WHERE pet_id = '$findYourPet[id]' AND id = '$find_item[id]' AND game = '$game'");
				$status_line .= "<font color=\"$topAndBottomBG\">Oh no! Your $find_item2[item_name] has been broken!</font><br>";
			}
		}
	}
}

$enemyAttack = explode(", ", $findEnemy[attack_pattern]);
$countAttack = count($enemyAttack) - 1;
$randAttack = rand(0,$countAttack);
$doAttack = $enemyAttack[$randAttack];

$findAttack2 = fetch("SELECT * FROM attacks2 WHERE attack_name = '$doAttack' AND game = '$game'");
if ($findAttack2[do_what] == "attack")
{
	$hit_or_not3 = rand(1,100);
	if ($hit_or_not3 > $findBattle[e_acc])
	{
		$status_line .= "<font color=\"#CC0000\">$findEnemy[name] tried to use $findAttack2[attack_name], but it failed.</font><br>";
	}
	if ($hit_or_not3 <= $findBattle[e_acc])
	{
		$randD1 = rand(1,2);
		$randD2 = rand(0,9);
		$divider = "$randD1.$randD2";
		$randD2 = rand(1,2);
		$randD2 = rand(0,9);
		$divider2 = "$randD1.$randD2";

		$do_damage2 = round(((($findEnemy[strength] * .65) * $findAttack2[magic_number]) * 	$divider) * .6);
		$do_damage2 = $do_damage2 - floor(($findYourPet[defense] * .4) * $divider2);
		$do_damage2 = round($do_damage2 / 2.5);

		if ($do_damage2 < 1) { $do_damage2 = 1; }
		$status_line .= "<font color=\"#CC0000\">$findEnemy[name] used $findAttack2[attack_name] and they did $do_damage2 points of damage.</font><br>";
	}
}
if ($findAttack2[do_what] == "defend")
{
	$hit_or_not4 = rand(1,100);
	if ($hit_or_not4 > $findBattle[e_acc])
	{
		$status_line .= "<font color=\"#CC0000\">$findEnemy[name] tried to use $findAttack2[attack_name], but it failed.</font><br>";
	}
	if ($hit_or_not4 <= $findBattle[e_acc])
	{
		$randD1 = rand(1,2);
		$randD2 = rand(0,9);
		$divider = "$randD1.$randD2";
		$takeOffExtra2 = round($findAttack2[magic_number] * $divider);
		$status_line .= "<font color=\"#CC0000\">$findEnemy[name] used $findAttack2[attack_name] and defended themself from $takeOffExtra2 points of damage.</font><br>";
	}
}

$do_damage -= $takeOffExtra2;
$do_damage2 -= $takeOffExtra;

if ($do_damage < 0) { $do_damage = 0; }
if ($do_damage2 < 0) { $do_damage2 = 0; }

$status_line .= "<font color=\"#0000FF\">Overall: $findEnemy[name] did $do_damage2 damage to you. You did $do_damage to $findEnemy[name].</font><br>";

if ($findYourPet[speed] > $findEnemy[speed])
{
	mysql_query("UPDATE one_player_battles2 SET enemy_hp=$findBattle[enemy_hp]-$do_damage WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE user_pets2 SET current_hp=$findYourPet[current_hp]-$do_damage2 WHERE owner = '$userid' AND id = '$findYourPet[id]' AND game = '$game'");
}

if ($findYourPet[speed] <= $findEnemy[speed])
{
	mysql_query("UPDATE user_pets2 SET current_hp=$findYourPet[current_hp]-$do_damage2 WHERE owner = '$userid' AND id = '$findYourPet[id]' AND game = '$game'");
	mysql_query("UPDATE one_player_battles2 SET enemy_hp=$findBattle[enemy_hp]-$do_damage WHERE userid = '$userid' AND game = '$game'");
}

mysql_query("UPDATE one_player_battles2 SET status_line = '$status_line' WHERE userid = '$userid' AND game = '$game'");

header("Location: battling_1p.php?game=$game");

?>